package com.tss.dao.impl;

import com.tss.dao.TicketTypeDAO;
import com.tss.model.TicketType;
import com.tss.utils.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * 车票类型表操作实现类
 */
public class TicketTypeDAOImpl implements TicketTypeDAO {

    @Override
    public List<TicketType> selectByType(String type) {
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        List<TicketType> ticketTypes = new ArrayList<>();
        
        try {
            conn = DBUtil.getConnection();
            String sql = "SELECT * FROM ticket_type WHERE ticket_type = ?";
            ps = conn.prepareStatement(sql);
            ps.setString(1, type);
            rs = ps.executeQuery();
            
            while (rs.next()) {
                TicketType ticketType = new TicketType();
                ticketType.setId(rs.getInt("ticket_id"));
                ticketType.setType(rs.getString("ticket_type"));
                ticketType.setCategory(rs.getString("category"));
                ticketType.setPrice(rs.getDouble("price"));
                ticketType.setValidityDays(rs.getInt("validity_days"));
                ticketTypes.add(ticketType);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.closeResources(null, ps, rs);
        }
        
        return ticketTypes;
    }

    @Override
    public List<TicketType> selectAll() {
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        List<TicketType> ticketTypes = new ArrayList<>();
        
        try {
            conn = DBUtil.getConnection();
            String sql = "SELECT * FROM ticket_type";
            ps = conn.prepareStatement(sql);
            rs = ps.executeQuery();
            
            while (rs.next()) {
                TicketType ticketType = new TicketType();
                ticketType.setId(rs.getInt("ticket_id"));
                ticketType.setType(rs.getString("ticket_type"));
                ticketType.setCategory(rs.getString("category"));
                ticketType.setPrice(rs.getDouble("price"));
                ticketType.setValidityDays(rs.getInt("validity_days"));
                ticketTypes.add(ticketType);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.closeResources(null, ps, rs);
        }
        
        return ticketTypes;
    }
} 